<template>
    <Upload
        type="drag"
        :format="['xls', 'xlsx']"
        :show-upload-list="false"
        :before-upload="onUploadBefore"
        :on-success="onUploadSuccess"
        :on-format-error="onUploadFormatError"
        accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
        :action="url">
        <div class="upload-content">
            <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
            <p>点击或将文件拖拽到这里上传</p>
            <slot name="content"></slot>
        </div>
    </Upload>
</template>

<script>
    export default {
        props: {
            url: {
                type: String,
                default: ''
            },
            item: {
                type: Object,
                default: {}
            },
            index: {
                type: Number,
                default: 0
            }
        },
        data() {
            return {
                status: 'pass'
            }
        },
        methods: {
            onUploadBefore() {
                this.item.status = 'loading';
            },
            onUploadSuccess(res, file) {
                this.item.status = 'pass';
                if (res.code === 200) {
                    let param = {
                        businessUploadType: this.item.type,
                        checkResult: '',
                        pageSize: this.item.pageSize,
                        currentPage: this.item.currentPage
                    }
                    this.$ajaxGet('/store/system/excel_import_data/query', param).then(data => {
                        this.item.checkResult = ''
                        this.$emit('complete', data, this.index);
                    })
                    file.url = res.data;
                    this.model = false;
                } else {
                    file.status = 'error';
                    this.$Notice.error({
                        title: '上传失败',
                        desc: res.message
                    });
                }
            },
            onUploadFormatError(file) {
                this.$Notice.warning({
                    title: '文件格式不正确',
                    desc: '文件 ' + file.name + ' 格式不正确，请上传 xls 或 xlsx 格式的图片。'
                });
                this.status = 'pass';
            }
        }
    }
</script>
